package cn.wolfcode.rbac.service.system_manage.impl;


import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.mapper.system_manage.PermissionMapper;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.system_manage.IPermissionService;
import cn.wolfcode.rbac.util.PageResult;
import cn.wolfcode.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author:Cao Zhijian
 * @DATE:2020/12/24 0024 14:40:32
 */

@Service
public class IPermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject queryObject) {

        int count = permissionMapper.selectForCount();

        if(count == 0){
            // 1.如果数据库中没有数据是不需要分页显示，直接返回一个空数据
            return new PageResult(1,3,Collections.emptyList(),0);
        }else {
            // 2.如果数据库中有数据就要分页显示
            List<Permission> permissions = permissionMapper.selectForList(queryObject);
            return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),permissions,count);
        }
    }

    @Override
    public void saveOrUpdate(Permission permission) {
        if(permission.getId() == null){
            permissionMapper.insert(permission);
        }else {
            permissionMapper.updateByPrimaryKey(permission);
        }
    }

    @Autowired
    private ApplicationContext context;

    /**
     *  加载所有controller中，只要方法上有@RequiredPermission这个注解的，就把这个注解里的信息封装到
     *  一个Permission对象中并且把他保存到数据库中
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/25 0025 11:05
     * @return : void
     */
    @Override
    public void reload() {

        List<String> list = permissionMapper.selectExpressions();

        // 1.先拿到所有的Controller
        Map<String, Object> beans = context.getBeansWithAnnotation(Controller.class);
        for(Object controller : beans.values()){
            // 2.拿到所有方法
            Method[] methods = controller.getClass().getMethods();
            for(Method method : methods){
                // 拿到方法上的注解
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                if(annotation != null && !list.contains(annotation.value()[1])){
                    String name = annotation.value()[0];
                    String expression = annotation.value()[1];
                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setExpression(expression);
                    // 保存到数据库中
                    permissionMapper.insert(permission);
                }
            }
        }
    }
}
